package com.cx.util.device;

import android.content.Context;
import android.graphics.Rect;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.View;
import android.view.WindowManager;

import com.cx.util.AppUtils;

public class SizeHelper {

    /**
     * transform dip to px
     *
     * @param dipValue the value of dip
     * @return px
     */
    public static float dip2px(float dipValue) {
        return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
                dipValue,
                AppUtils.getApp().getResources().getDisplayMetrics());
    }

    /**
     * transform sp to px
     *
     * @param spValue spValue
     * @return px
     */
    public static float sp2px(float spValue) {
        return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,
                spValue,
                AppUtils.getApp().getResources().getDisplayMetrics());
    }


    /**
     * get this view location on screen
     *
     * @param view view
     * @return rect
     */
    public static Rect getRectOnScreen(View view) {
        int[] location = getLocationOnScreen(view);
        int left = location[0];
        int top = location[1];
        int right = left + view.getMeasuredWidth();
        int bottom = top + view.getMeasuredHeight();
        return new Rect(left, top, right, bottom);
    }

    /**
     * get this view position on screen
     *
     * @param view view
     * @return int[]
     */
    public static int[] getLocationOnScreen(View view) {
        int[] location = new int[2];
        view.getLocationOnScreen(location);
        return location;
    }



    /**
     * get device screen size
     * @return DisplayMetrics
     */
    public static DisplayMetrics getScreenSize() {
        Context context = AppUtils.getApp();
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics metrics = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(metrics);
        return metrics;
    }

}
